package com.base.task;

import com.base.task.em.BatchTypeEm;
import com.base.task.entity.BatchResult;
import com.base.task.entity.BatchParam;
import lombok.extern.slf4j.Slf4j;

import java.time.Duration;
import java.time.LocalDateTime;

/**
 * @ Author     ：山水之间-wxs
 * @ Description：批次执行
 * @ Date       ：Created in 10:52 2021/11/4
 * @ Modified By：wxs
 * @ Version    ：1.0
 */
@Slf4j
public class BatchRunTask extends AbstractBatchServerTarget {

    public final int totalCount(BatchTypeEm type, BatchParam param) {
        return init(type, param).getTotalCount();
    }

    public final int totalCount(BatchTypeEm type, String title) {
        return init(type, new BatchParam(title)).getTotalCount();
    }

    public BatchResult runTask(BatchTypeEm type, String title) {
        return runTask(type, new BatchParam(title));
    }

    public BatchResult runTask(BatchTypeEm type, BatchParam param) {
        log.info("[" + type + "-BATCH]:{}#{}..开始！！！", param.getTitle(), LocalDateTime.now());
        LocalDateTime bgnTime = LocalDateTime.now();
        BatchResult result = init(type, param).runTask();
        Duration duration = Duration.between(bgnTime, LocalDateTime.now());
        log.info("[" + type + "-BATCH]:{}#{}..结束！！！==跑批总数：{}，成功总数：{}，总耗时：{}s", param.getTitle(), LocalDateTime.now(),
                result.getTotalCount(), result.getSuccessTotalCount(), duration.getSeconds());
        return result;
    }

}
